#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int minLength(char* s) {
    int n = strlen(s), m = 0;
    char* a = (char*)malloc(sizeof(char) * n);
    for (int i = 0; i < n; i++)
    {
        a[m++] = s[i];
        if (m >= 2 && (a[m - 2] == 'A' && a[m - 1] == 'B'
            || a[m - 2] == 'C' && a[m - 1] == 'D'))
        {
            m -= 2;
        }
    }
    free(a);
    return m;
}